package com.kylin.common.weixin.mp.bean.template;

import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 模版消息实体类
 *
 * @author wuhao
 * @version 1.0 - 2019-03-22
 */
public class Template {

    private String touser;
    private String templateId;
    private String url;
    private String topcolor;

    private MiniProgram miniProgram;
    private List<TemplateData> datas = new ArrayList<TemplateData>();

    public void setData(TemplateData data){
        this.datas.add(data);
    }

    public String getTouser() {
        return touser;
    }

    public void setTouser(String touser) {
        this.touser = touser;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTopcolor() {
        return topcolor;
    }

    public void setTopcolor(String topcolor) {
        this.topcolor = topcolor;
    }

    public MiniProgram getMiniProgram() {
        return miniProgram;
    }

    public void setMiniProgram(MiniProgram miniProgram) {
        this.miniProgram = miniProgram;
    }

    public List<TemplateData> getDatas() {
        return datas;
    }

    public void setDatas(List<TemplateData> datas) {
        this.datas = datas;
    }

    /**
     * 转换成接口格式
     * @param
     * @return
     * @version 1.0  by wuhao at 2019-03-22
     */
    public JSONObject toJSON(){
        JSONObject json = new JSONObject();
        json.put("touser", this.touser);
        json.put("template_id", this.templateId);
        json.put("url", this.url);
        json.put("topcolor", this.topcolor);
        json.put("miniprogram", JSONObject.toJSONString(this.miniProgram));
        JSONObject data = new JSONObject();
        for (int i = 0; i < this.datas.size(); i++) {
            JSONObject remark = new JSONObject();
            remark.put("value", datas.get(i).getValue());
            remark.put("color", datas.get(i).getColor());
            data.put(datas.get(i).getName(), remark);
        }
        if(data != null){
            json.put("data", data);
        }
        return json;
    }
}
